package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TImonthinfo;
import com.zmht.specedu.vo.Teacher;

public class IMonthinfoAction extends BaseAction {

	/*
	 * 初始化个别月主题添加页面
	 * (non-Javadoc)
	 * @see org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward initIMonthinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
			
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		//获得集体目标对应的学生
		List list = this.getCareaContentBiz().getCareaContentDAO().findBySql("select s.StudentId,s.SName,s.ClassID from classteacher ct,student s where s.ClassID=ct.ClassID and TeacherID = "+teacher.getTeacherId());
	
		request.setAttribute("teacherName", teacher.getTname());
		request.setAttribute("students", list);
		
		return mapping.findForward("newjiaoxue");
	}
	/**
	 * 获得当前月的目标内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getIMonthinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String month = "1";//request.getParameter("month");
		// 开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		
		List<TImonthinfo> list = this.getImonthinfoBiz().getImonthinfo(teacher.getTeacherId(), new Integer(month), (index-1)*pageSize, pageSize);
		int total = this.getImonthinfoBiz().getImonthinfoTotal(teacher.getTeacherId(), new Integer(month));
		
		
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("month", month);
		
		request.setAttribute("monthinfos", list);
		request.setAttribute("tname", teacher.getTname());
		//return mapping.findForward("newjiaoxue_content");
		return mapping.findForward("gebieyuezhuti");
	}
	/**
	 * 保存记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveIMonthinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String month = request.getParameter("month");
		String imonthContent = request.getParameter("imonthContent");
		String monthContent = request.getParameter("monthContent");
		
		
		TImonthinfo monthinfo = new TImonthinfo();
		monthinfo.setTeacherId(teacher.getTeacherId());
		monthinfo.setImonthContent(imonthContent);
		monthinfo.setMonthType(new Integer(month));
		monthinfo.setMonthName(monthContent);//月主题名称
		
		this.getImonthinfoBiz().saveImonthinfo(monthinfo);
		request.setAttribute("monthContent", monthContent);
		return new ActionForward("/beike/imonthinfo.do?method=getIMonthinfo&month="+month);
	}
	/**
	 * 删除记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delIMonthinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String month = request.getParameter("month");
		String id = request.getParameter("id");
		System.out.println("执行删除。。。。。"+id+"  month="+month);
		this.getImonthinfoBiz().deleteImonthinfo(new Integer(id));
		
		return new ActionForward("/beike/imonthinfo.do?method=getIMonthinfo&month="+month);
	}
}
